using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.Win32;


namespace RandomRat {



	/// <summary>
	/// Summary description for Settings.
	/// </summary>
	public class Settings : System.Windows.Forms.Form {
		
		DataManager dataManager;
		Form1 form1;

		#region WINVARS

		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TabPage tabPage1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Button butCancel;
		private System.Windows.Forms.Button butOK;
		private System.Windows.Forms.Button butSelect;
		private System.Windows.Forms.RadioButton radBlankCriteria;
		private System.Windows.Forms.RadioButton radSavedCriteria;
		private System.Windows.Forms.TextBox txtCriteria;
		private System.Windows.Forms.CheckBox chkShowQuickStart;
		private System.ComponentModel.Container components = null;

		#endregion

		public Settings(DataManager dataManager, Form1 form1) {
			InitializeComponent();
			this.dataManager = dataManager;
			this.form1 = form1;
			
			if (ApplicationSettings.StartupCriteria == StartupCriteriaType.Blank) {
				radBlankCriteria.Checked = true;
			} else {
				radSavedCriteria.Checked = true;
			}

			txtCriteria.Text = ApplicationSettings.SpecifiedCriteria;
			chkShowQuickStart.Checked = ApplicationSettings.ShowQuickStart;

			validateControls();
		}


		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.tabPage1 = new System.Windows.Forms.TabPage();
			this.chkShowQuickStart = new System.Windows.Forms.CheckBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.butSelect = new System.Windows.Forms.Button();
			this.txtCriteria = new System.Windows.Forms.TextBox();
			this.radSavedCriteria = new System.Windows.Forms.RadioButton();
			this.radBlankCriteria = new System.Windows.Forms.RadioButton();
			this.butCancel = new System.Windows.Forms.Button();
			this.butOK = new System.Windows.Forms.Button();
			this.tabControl1.SuspendLayout();
			this.tabPage1.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// tabControl1
			// 
			this.tabControl1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					  this.tabPage1});
			this.tabControl1.Location = new System.Drawing.Point(7, 7);
			this.tabControl1.Name = "tabControl1";
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = new System.Drawing.Size(372, 288);
			this.tabControl1.TabIndex = 0;
			// 
			// tabPage1
			// 
			this.tabPage1.Controls.AddRange(new System.Windows.Forms.Control[] {
																				   this.chkShowQuickStart,
																				   this.groupBox1});
			this.tabPage1.Location = new System.Drawing.Point(4, 22);
			this.tabPage1.Name = "tabPage1";
			this.tabPage1.Size = new System.Drawing.Size(364, 262);
			this.tabPage1.TabIndex = 0;
			this.tabPage1.Text = "Startup";
			// 
			// chkShowQuickStart
			// 
			this.chkShowQuickStart.Checked = true;
			this.chkShowQuickStart.CheckState = System.Windows.Forms.CheckState.Checked;
			this.chkShowQuickStart.Location = new System.Drawing.Point(16, 120);
			this.chkShowQuickStart.Name = "chkShowQuickStart";
			this.chkShowQuickStart.Size = new System.Drawing.Size(164, 24);
			this.chkShowQuickStart.TabIndex = 1;
			this.chkShowQuickStart.Text = "Show quick start at startup";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.butSelect,
																					this.txtCriteria,
																					this.radSavedCriteria,
																					this.radBlankCriteria});
			this.groupBox1.Location = new System.Drawing.Point(15, 20);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(336, 84);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Startup criteria";
			// 
			// butSelect
			// 
			this.butSelect.Enabled = false;
			this.butSelect.Location = new System.Drawing.Point(256, 47);
			this.butSelect.Name = "butSelect";
			this.butSelect.Size = new System.Drawing.Size(60, 20);
			this.butSelect.TabIndex = 3;
			this.butSelect.Text = "Select";
			this.butSelect.Click += new System.EventHandler(this.butSelect_Click);
			// 
			// txtCriteria
			// 
			this.txtCriteria.Location = new System.Drawing.Point(108, 47);
			this.txtCriteria.Name = "txtCriteria";
			this.txtCriteria.ReadOnly = true;
			this.txtCriteria.Size = new System.Drawing.Size(144, 20);
			this.txtCriteria.TabIndex = 2;
			this.txtCriteria.Text = "";
			// 
			// radSavedCriteria
			// 
			this.radSavedCriteria.Location = new System.Drawing.Point(12, 46);
			this.radSavedCriteria.Name = "radSavedCriteria";
			this.radSavedCriteria.Size = new System.Drawing.Size(96, 24);
			this.radSavedCriteria.TabIndex = 1;
			this.radSavedCriteria.Text = "Saved criteria:";
			this.radSavedCriteria.CheckedChanged += new System.EventHandler(this.someChk_Changed);
			// 
			// radBlankCriteria
			// 
			this.radBlankCriteria.Checked = true;
			this.radBlankCriteria.Location = new System.Drawing.Point(12, 20);
			this.radBlankCriteria.Name = "radBlankCriteria";
			this.radBlankCriteria.Size = new System.Drawing.Size(144, 24);
			this.radBlankCriteria.TabIndex = 0;
			this.radBlankCriteria.TabStop = true;
			this.radBlankCriteria.Text = "Blank untitled critiera";
			this.radBlankCriteria.CheckedChanged += new System.EventHandler(this.someChk_Changed);
			// 
			// butCancel
			// 
			this.butCancel.Location = new System.Drawing.Point(304, 304);
			this.butCancel.Name = "butCancel";
			this.butCancel.TabIndex = 1;
			this.butCancel.Text = "Cancel";
			this.butCancel.Click += new System.EventHandler(this.butCancel_Click);
			// 
			// butOK
			// 
			this.butOK.Location = new System.Drawing.Point(220, 304);
			this.butOK.Name = "butOK";
			this.butOK.TabIndex = 2;
			this.butOK.Text = "OK";
			this.butOK.Click += new System.EventHandler(this.butOK_Click);
			// 
			// Settings
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(388, 334);
			this.ControlBox = false;
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.butOK,
																		  this.butCancel,
																		  this.tabControl1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Settings";
			this.ShowInTaskbar = false;
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Settings";
			this.tabControl1.ResumeLayout(false);
			this.tabPage1.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion


		private void someChk_Changed(object sender, System.EventArgs e) {
			validateControls();
		}

		private void validateControls() {
			if (radSavedCriteria.Checked) {
				butSelect.Enabled = true;
				txtCriteria.Enabled = true;
			} else {
				butSelect.Enabled = false;
				txtCriteria.Enabled = false;
			}
		}


		private void butSelect_Click(object sender, System.EventArgs e) {
			// Show criteria selector
			CriteriaSelector criteriaSelector = new CriteriaSelector(dataManager, form1);
			criteriaSelector.Text = "Criteria Selector";

			criteriaSelector.ShowDialog();

			if (criteriaSelector.DialogResult == DialogResult.OK) {	
				txtCriteria.Text = criteriaSelector.SelectedCritieriaID;
			}
			criteriaSelector.Dispose();
		}

		private void butOK_Click(object sender, System.EventArgs e) {
			ApplicationSettings.SpecifiedCriteria = txtCriteria.Text.Trim();
			ApplicationSettings.ShowQuickStart = chkShowQuickStart.Checked;

			ApplicationSettings.StartupCriteria = 
				radBlankCriteria.Checked ? StartupCriteriaType.Blank : StartupCriteriaType.Specified;

			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void butCancel_Click(object sender, System.EventArgs e) {
			this.Close();
		}
	}

    public enum StartupCriteriaType { Blank, Specified };

    public class ApplicationSettings
    {

        public const string baseKey = "Software\\masinc\\RandomRat";

        public static StartupCriteriaType StartupCriteria
        {
            get
            {
                RegistryKey key = getKet(false);
                object scType = key.GetValue("StartupCritiera");

                if (scType != null)
                {
                    return (StartupCriteriaType)scType;
                }
                else
                {
                    return StartupCriteriaType.Blank; // defult
                }
            }

            set
            {
                getKet(true).SetValue("StartupCritiera", (int)value);
            }
        }

        public static string SpecifiedCriteria
        {
            get
            {
                RegistryKey key = getKet(false);
                object sCriteria = key.GetValue("SpecifiedCriteria");

                if (sCriteria != null)
                {
                    return (string)sCriteria;
                }
                else
                {
                    return ""; // defult
                }
            }

            set
            {
                getKet(true).SetValue("SpecifiedCriteria", value);
            }
        }

        public static bool ShowQuickStart
        {
            get
            {
                RegistryKey key = getKet(false);

                object showQS = key.GetValue("ShowQuickStart");

                if (showQS != null)
                {
                    return (int)showQS == 1;
                }
                else
                {
                    return true; // defult
                }
            }

            set
            {
                getKet(true).SetValue("ShowQuickStart", value ? 1 : 0);
            }
        }

        private static RegistryKey getKet(bool writable)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(baseKey, writable);
            if (key == null) key = Registry.CurrentUser.CreateSubKey(baseKey);
            return key;
        }

    }

}
